#include <gtk/gtk.h>
#include <stdio.h>

#include "system.h"
#include "proc/self.h"

#include "gtkutils.h"


gboolean deal_time( gpointer* user_data )   
{  
    GSList *list = (GSList*)user_data;
    System sys = {0};
    initSystem(&sys);
    
    gtk_label_set_text((GTK_LABEL(g_slist_nth(list, 0)->data)),sys.Kernel_Version);
    gtk_label_set_text((GTK_LABEL(g_slist_nth(list, 1)->data)),sys.Architecture);
    gtk_label_set_text((GTK_LABEL(g_slist_nth(list, 2)->data)),sys.Systime);

    SelfStatusInfo info = {0};
    if (initSelfStatusInfo(&info)) {
        INFO_LOG("Name: %s",info.Name);
        INFO_LOG("Umask: %s",info.Umask);
        INFO_LOG("State: %s",info.State);
        INFO_LOG("Tgid: %s",info.Tgid);
        INFO_LOG("Ngid: %s",info.Ngid);
        INFO_LOG("Pid: %s",info.Pid);
        INFO_LOG("PPid: %s",info.PPid);

        INFO_LOG("----------------------");

        DEBUG_LOG("VmPeak: %.2f mb", info.VmPeak / 1024.0);
        DEBUG_LOG("VmSize: %.2f mb", info.VmSize / 1024.0);
        DEBUG_LOG("VmLck: %.2f mb", info.VmLck / 1024.0);
        DEBUG_LOG("VmPin: %.2f mb", info.VmPin / 1024.0);
        DEBUG_LOG("VmHWM: %.2f mb", info.VmHWM / 1024.0);
        DEBUG_LOG("VmRSS: %.2f mb", info.VmRSS / 1024.0);
    }

    return TRUE;//尽量返回TRUE  
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWindow *window;
    GtkButton *button;

    window = createWindowWithTitleSize(app, "gtk-system-info-test", 200, 100);

    GtkBox *box1 = createBoxWithVertical(5);

    GtkLabel *label1 = createLabel("");
    GtkLabel *label2 = createLabel("");
    GtkLabel *label3 = createLabel("");

    GSList *list = NULL;
    list = g_slist_append(list, label1);
    list = g_slist_append(list, label2);
    list = g_slist_append(list, label3);
    createTimerSecond(1, deal_time, list);

    boxAppend(box1, label1);
    boxAppend(box1, label2);
    boxAppend(box1, label3);


    setWindowChild(window, box1);
    showWindow(window);
}

int	main(int argc, char **argv) {
    GtkApplication *app = createApplicationConnectActivate("editor.tr", 
                                                        G_APPLICATION_FLAGS_NONE, 
                                                        activate);
    int status = runApplication(app, argc, argv);
    objectUnref(app);
    return status;
}

